//
//  ChatStore.m
//  NewPro
//
//  Created by RYX on 2019/2/15.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "ChatStore.h"
#import "ChatModel.h"

@implementation ChatStore

/**
 * 查询客服是否在线
 */
- (void)serviceOnlineSuccess:(void(^)(BOOL onLine,NSString *tip))success Failure:(void(^)(NSError *error))failure{
    
    NSString *url = [NSString string];
    
    #ifdef DEBUG
    
        url = [NSString stringWithFormat:@"http://120.27.239.20:8083/service/v1/online"];
    #else
    
        url = [NSString stringWithFormat:@"http://www.forcard.store:8083/service/v1/online"];
    #endif
    
    [HttpTool getUrlWithString:url parameters:nil success:^(id responseObject) {
        
        NSError *error = [HttpTool inspectError:responseObject];
        if (error == nil) {
            
            BOOL online = NO;
            if ([responseObject[@"data"][@"online"] intValue] == 1) {
                
                online = YES;
            }
            
            NSString *tip = responseObject[@"data"][@"tip"];
            
            success(online,tip);
            
        }else{
            
            failure(error);
        }
        
    } failure:^(NSError *error) {
       
        failure(error);
    }];
}

#pragma mark - 查询消息是否发送成功
- (void)checkInfoIfSendSuccess:(void(^)(BOOL hadSend))success Failure:(void(^)(NSError *error))failure{
    
    
}

#pragma mark - 通知服务器消息已读
- (void)notiServerHadReadInfoWithSign:(NSString *)sign Success:(void(^)(void))success Failure:(void(^)(NSError *error))failure{
    
    NSString *url = [NSString string];
                     
#ifdef DEBUG
    
    url = [NSString stringWithFormat:@"http://120.27.239.20:8083/receive/%@",sign];
#else
    
    url = [NSString stringWithFormat:@"http://www.forcard.store:8083/receive/%@",sign];
#endif
    
    [HttpTool getUrlWithString:url parameters:nil success:^(id responseObject) {
        
        NSError *error = [HttpTool inspectError:responseObject];
        
        if (error == nil) {
            
            
            success();
        }else{
            
            failure(error);
        }
        
    } failure:^(NSError *error) {
       
        failure(error);
    }];
}

#pragma mark - 获取未读消息
- (void)searchNoReadMsgWithCustomerSign:(NSString *)customerSign Sucess:(void(^)(NSArray *arr))success Failure:(void(^)(NSError *error))failure{
    
    NSString *url = [NSString string];
    
#ifdef DEBUG
    
    url = [NSString stringWithFormat:@"http://120.27.239.20:8083/customer/unread/%@",customerSign];
#else
    
    url = [NSString stringWithFormat:@"http://www.forcard.store:8083/customer/unread/%@",customerSign];
#endif
    
    NSString *urlStr = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    [HttpTool getUrlWithString:urlStr parameters:nil success:^(id responseObject) {
        
        NSError *error = [HttpTool inspectError:responseObject];
        
        if (error == nil) {
            
            NSArray *arr = [ChatModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            success(arr);
        }else{
            
            failure(error);
        }
        
    } failure:^(NSError *error) {
       
        failure(error);
    }];
}
@end
